package net.cyue.ort.llm.gui;

import net.cyue.ort.llm.gui.chat.ChatController;
import net.cyue.ort.llm.gui.chat.ChatPanel;
import net.cyue.ort.llm.gui.config.ModelConfigPanel;
import net.cyue.ort.llm.gui.config.ModelLoader;
import net.cyue.ort.llm.gui.state.AppState;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Main::launch);
    }

    private static void launch() {
        JFrame frame = createMainFrame();
        AppState appState = new AppState();
        ChatController chatController = new ChatController(appState);
        ModelLoader modelLoader = new ModelLoader(appState);

        ChatPanel chatPanel = new ChatPanel(frame, chatController);
        ModelConfigPanel configPanel = new ModelConfigPanel(frame, modelLoader);

        frame.add(chatPanel, BorderLayout.CENTER);
        frame.add(configPanel, BorderLayout.EAST);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                modelLoader.close();
                chatController.close();
                appState.close();
            }
        });

        frame.setVisible(true);
    }

    private static JFrame createMainFrame() {
        JFrame frame = new JFrame("ORT LLM Chat");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setBounds(200, 100, 1000, 700);
        frame.setLayout(new BorderLayout());

        JPanel containerPanel = new JPanel(new BorderLayout());
        containerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        frame.setContentPane(containerPanel);

        return frame;
    }
}